package com.data.monitoring.controller;

import com.data.monitoring.model.DTO.FolderInitDTO;
import com.data.monitoring.model.DTO.ServerDTO;
import com.data.monitoring.model.ENUM.AuthMethod;
import com.data.monitoring.model.entity.MonitoredFolders;
import com.data.monitoring.model.entity.Servers;
import com.data.monitoring.service.MonitoredFoldersService;
import com.data.monitoring.service.ServersService;
import com.data.monitoring.service.impl.FolderMonitorScheduler;
import com.data.monitoring.utils.EncryptionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务器基本信息 前端控制器
 * </p>
 *
 * @author HanYiMing
 * @since 2025-05-28
 */
@RestController
@RequestMapping("/api/servers")
@Api(tags = "服务器控制类")
public class ServersController {

    @Autowired
    private ServersService serversService;

    @Autowired
    private FolderMonitorScheduler monitorScheduler;

    @Autowired
    private MonitoredFoldersService monitoredFoldersService;

    @Autowired
    private EncryptionUtil encryptionUtil;

    /**
     * 添加新服务器
     */
    @PostMapping("addServer")
    @ApiOperation(value = "添加服务器信息")
    public ResponseEntity<Servers> addServer(@Valid @RequestBody ServerDTO dto) {
        Servers server = new Servers();
        server.setServerName(dto.getServerName());
        server.setIpAddress(dto.getIpAddress());
        server.setSshPort(dto.getSshPort());
        server.setUsername(dto.getUsername());
        server.setAuthMethod(dto.getAuthMethod());

        // 加密存储认证信息
        if (dto.getAuthMethod().equals(AuthMethod.PASSWORD.getValue())) {
            server.setAuthKey(encryptionUtil.encrypt(dto.getPassword()));
        } else {
            server.setAuthKey(encryptionUtil.encrypt(dto.getPrivateKey()));
        }

        serversService.save(server);
        return ResponseEntity.created(URI.create("/api/servers/" + server.getServerId()))
                .body(server);
    }

    /**
     * 初始化服务器监控任务
     */
    @PostMapping("/{serverId}/init-monitoring")
    @ApiOperation(value = "初始化服务器监控任务")
    public ResponseEntity<?> initMonitoring(
            @PathVariable Long serverId,
            @RequestBody List<FolderInitDTO> folders) {

        Servers server = serversService.getById(serverId);

        List<MonitoredFolders> savedFolders = new ArrayList<>();

        for (FolderInitDTO dto : folders) {
            MonitoredFolders folder = new MonitoredFolders();
            folder.setServers(server);
            folder.setServerId(server.getServerId());
            folder.setFolderPath(dto.getFolderPath());
            folder.setCheckInterval(dto.getCheckInterval());
            folder.setMaxDepth(dto.getMaxDepth());
            folder.setActive(true);
            monitoredFoldersService.save(folder);
            savedFolders.add(folder);
            monitorScheduler.scheduleMonitor(folder);
        }

        return ResponseEntity.ok(savedFolders);
    }


}
